package com.liunian.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.liunian.subject.application.dto.SubjectCategoryDTO;
import com.liunian.subject.common.entity.Result;
import com.liunian.subject.domain.entity.SubjectCategoryBO;
import com.liunian.subject.domain.service.SubjectCategoryDomainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/subject/category")
@Api(tags = "刷题分类controller")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    @ApiOperation(value = "新增分类")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()), "分类名不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类父级id不能为空");
            SubjectCategoryBO subjectCategoryBO = subjectCategoryDTO.subjectCategoryDTOtoBO();
            return Result.success(subjectCategoryDomainService.add(subjectCategoryBO));
        } catch (Exception e) {
            return Result.fail("新增分类失败");
        }
    }

    @PostMapping("/queryPrimaryCategory")
    @ApiOperation(value = "查询题目大类")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = subjectCategoryDTO.subjectCategoryDTOtoBO();
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> dtoList = boList.stream().map(subjectCategoryDTO::subjectCategoryBOToDTO).collect(Collectors.toList());
            return Result.success(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败！");
        }
    }

    @PostMapping("/queryCategoryByPrimary")
    @ApiOperation(value = "根据题目大类查询题目类型")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO bo = subjectCategoryDTO.subjectCategoryDTOtoBO();
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategory(bo);
            List<SubjectCategoryDTO> dtoList = boList.stream().map(subjectCategoryDTO::subjectCategoryBOToDTO).collect(Collectors.toList());
            return Result.success(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return Result.fail("查询失败！");
        }
    }


    @PostMapping("/update")
    @ApiOperation(value = "更新分类")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO bo = subjectCategoryDTO.subjectCategoryDTOtoBO();
            return Result.success(subjectCategoryDomainService.update(bo));
        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新分类失败！");
        }
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除分类")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO bo = subjectCategoryDTO.subjectCategoryDTOtoBO();
            return Result.success(subjectCategoryDomainService.delete(bo));
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}", e.getMessage(), e);
            return Result.fail("刪除分类失败！");
        }
    }



}
